package com.jb.repair.util;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.jb.repair.R;
import com.jb.repair.common.AppConfig;
import com.jb.repair.common.Constants;

import com.jb.repair.hardware.camera.Intents;
import com.jb.repair.net.HttpCookies;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by dell on 2016/4/14.
 */
public class DownFileAsyncTask extends AsyncTask<Void, Void, Integer> {
    private Context context;
    private String guid;
    private String obj_id;
    private String lx;//附件类型
    private int flag = -1;
    private String filepath;

    public DownFileAsyncTask(Context context, String guid, String obj_id, String lx) {
        this.context = context;
        this.guid = guid;
        this.obj_id = obj_id;
        this.lx = lx;
    }

    @Override
    protected Integer doInBackground(Void... params) {

        String s = AppConfig.BASE_URL + Constants.DOWN_FILE_URL;
        s = String.format(s, guid, obj_id);
        if(obj_id.equals("")){
            s=AppConfig.BASE_URL+"common/uploadGridFile/downloadFileItem.do?guid="+guid+"&zbGuid=''&tableName=US_APP.TB_EQ_FILES";
        }

        Log.i("ABC", "附件下载地址：" + s);

        try {

            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpRequest = new HttpGet(s);
            HttpCookies li = new HttpCookies(context);
            CookieStore cs = li.getuCookie();
            if (cs != null) {
                client.setCookieStore(li.getuCookie());
            }
            HttpResponse httpResponse = client.execute(httpRequest);
            int resCode = httpResponse.getStatusLine().getStatusCode();
            Log.i("ABC", resCode + "wwwwwwwwwwwww");

            if (resCode == 200) {
                HttpEntity entity = httpResponse.getEntity();
                int length = 0;
                InputStream in = entity.getContent();
                String path = AppConfig.ATTACHMENT_PATh;
                File file = new File(path);
                if (!file.exists()) {
                    file.mkdirs();
                }
                filepath = path + guid + "." + lx;
                //Log.i(TAG, "附件下载后存储路径" + filepath);
                File file1 = new File(filepath);
                FileOutputStream out = new FileOutputStream(file1);
                byte[] buff = new byte[1024 * 1024];
                while ((length = in.read(buff)) > 0) {
                    out.write(buff, 0, length);
                }
                in.close();
                out.flush();
                out.close();
                if (!file1.exists()) {
                    flag = 2;
                } else {
                    flag = 1;
                }

            } else {
                flag = 0;
            }

        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }


    @Override
    protected void onPostExecute(Integer i) {
        if (i == 1) {
            MessageUtil.showToast(context, "下载完成");
            IntentUtils.JumpToIntent(context, lx, filepath);
        } else if (i == 2) {
            MessageUtil.showToast(context, "没有附件");
        } else if (i == 0) {
            MessageUtil.showToast(context, "请重新连接网络");
        }


    }


}

